import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: "sk-0fa1648b8854488291b015f24ab00b6f", // 建议改为环境变量
  baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
  dangerouslyAllowBrowser: true
});

// 系统预设消息
const systemMessage = {
  role: "system",
  content: `你是一个助手,可以回答用户的任何问题，用中文回复`
};

export async function getAIResponse(userMessages) {
  try {
	  userMessages={
		  role:"user",
		  content:userMessages
	  // console.log(userMessages)
	  };
	 console.log(userMessages)
	  // console.log(1111)
	  // console.log(userMessages)
    const completion = await openai.chat.completions.create({
      model: "qwen-plus",
      messages: [systemMessage,userMessages],
      temperature: 0.7
    });
    // console.log(32)
    return completion.choices[0].message.content;
  } catch (error) {
    console.error("API请求失败:", error);
    return "服务暂时不可用，请稍后再试。紧急情况请直接就医。";
  }
}